﻿/**
 * 提示浮动层
 * @param {type} text
 */
module MsgBox {
    /**
     * 待移除的提示浮动层
     */
    var bootstrap_growl_remove: Array<any> = [];

    export var growl_default_options = {
        ele: "body",
        type: "info",
        allow_dismiss: true,
        position: {
            from: "top",
            align: "center"
        },
        offset: 20,
        spacing: 10,
        z_index: 99999,
        fade_in: 400,
        delay: 5000,
        pause_on_mouseover: true,

        template: {
            alertPrefix: "alert-",
            container: '<div class="col-sm-10 col-sm-10 col-md-3 alert">',
            dismiss: '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>',
            //title: '<strong>',
            //message: ''
        }
    };

    $.growl = (content: string, options: any) => {
        var message = content;
        options = $.extend(true, {}, growl_default_options, options);

        //options.template.icon = '<span class="">';

        var growlClass = "bootstrap-growl-" + options.position.from + "-" + options.position.align;
        var $growl = $(options.template.container);

        $growl.addClass(growlClass);
        $growl.addClass("bootstrap-growl");
        $growl.addClass(options.template.alertPrefix + options.type);
        $growl.append($(options.template.dismiss));

        //if (icon) {
        //    if (options.template.icon) {
        //            $growl.append($(options.template.icon).addClass(icon));
        //    } else {
        //        $growl.append(icon);
        //    }
        //}

        $growl.append(message);

        //if (options.template.message) {
        //    $growl.append($(options.template.message).html(message));
        //} else {

        //}
        var offsetAmount = options.offset;
        $("." + growlClass).remove();
        $("." + growlClass).each(function () {
            return offsetAmount = Math.max(offsetAmount, parseInt($(this).css(options.position.from)) + $(this).outerHeight() + options.spacing);
        });
        var css: any = {
            "position": options.ele === "body" ? "fixed" : "absolute",
            "margin": 0,
            "z-index": options.z_index,
            "display": "none"
        };
        css[options.position.from] = offsetAmount + "px";
        $growl.css(css);
        $(options.ele).append($growl);
        switch (options.position.align) {
            case "center":
                $growl.css({
                    "left":
                    "50%",
                    "marginLeft": -($growl.outerWidth() / 2) + "px"
                });
                break;
            case "left":
                $growl.css("left", options.offset + "px");
                break;
            case "right":
                $growl.css("right", options.offset + "px");
                break;
        }
        $("body").trigger("grow.show", event);

        var fadeIn = $growl.fadeIn(options.fade_in, (event: any) => {
            $("body").trigger("grow.shown", event);
            if (options.delay > 0) {
                if (options.pause_on_mouseover == true) {
                    $growl.on('mouseover', () => {
                        clearTimeout(bootstrap_growl_remove[$growl.index()]);
                    }).on('mouseleave', () => {
                        bootstrap_growl_remove[$growl.index()] = setTimeout(() => $growl.remove(),
                            options.delay);
                    });
                }
                bootstrap_growl_remove[$growl.index()] = setTimeout(() => $growl.remove(),
                    options.delay);
            }
        });
        return $growl;
    };

    /**
     * 配置模板
     * alertPrefix: "alert-", //颜色样式前缀
       container: '<div class="col-sm-10 col-sm-10 col-md-3 alert">', //主体模板容器
       dismiss: '<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>', //关闭按钮容器
     * @param options
     * 
     */
    export function SetConfig(options: any) {
        growl_default_options.template = $.extend(growl_default_options.template, options);
    }

    /**
     * 显示错误提示 (红色)
     * @param {type} text
     * @returns
     */
    export function Error(text: string) {
        return $.growl(text, { type: 'danger' });
    };

    /**
     * 显示信息提示 (蓝色)
     * @param {type} text
     * @returns
     */
    export function Info(text: string) {
        return $.growl(text, { type: 'info' });
    };

    /**
     * 显示成功提示 (绿色)
     * @param {type} text
     * @returns
     */
    export function Success(text: string) {
        return $.growl(text, { type: 'success' });
    };

    /**
     * 根据返回结果决定显示成功或错误提示
     * @param {type} result 数据结果
     * @param {type} successText? 成功后显示的文字，如果不填默认为result.Msg
     * @param {type} act? 成功后执行的方法，会将result.Data传入作为变量
     */
    export function Show(result: Sail.AjaxResult, successText?: any, act?: Function) {

        if (typeof (successText) == "function") {
            act = <Function>successText;
            successText = null;
        }
        if (result.IsSuccess)
            Success(successText || result.Msg);
        else
            Error(result.Msg);
        if (act && result.IsSuccess) {
            act(result.Data);
        }
    };


    /**
     * 操作成功才提示
     * @param {Sail.AjaxResult} result
     * @param {Function} act?
     */
    export function Action(result: Sail.AjaxResult, act?: Function, msg?: string) {
        if (result.IsSuccess) {
            if (act) act(result.Data);
        }
        else
            Error(msg || result.Msg);

    }
}
var ShowMessage = MsgBox.Show;
var ShowInfo = MsgBox.Info;
var ShowSuccess = MsgBox.Success;
var ShowError = MsgBox.Error;